package com.glink.manage.controller;


import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.code.*;
import com.glink.manage.service.CodeService;
import com.glink.manage.vo.code.CodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : qiushaoshan
 * @create 2025/1/7 18:23
 */
@RestController
@RequestMapping("/API/Manage/code")
@Api(tags = "公共代码")
@Slf4j
public class CodeController extends AbstractBaseController {

    @Autowired
    private CodeService codeService;
    
    @ApiOperation(value = "01.查询列表所有-非左侧树")
    @GetMapping(value = "/findLeaf", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<CodeVO>> findLeaf() {
        log.info("01.查询列表所有-非左侧树");
        List<CodeVO> codeVOList = codeService.getLeafList();
        log.info("01.查询列表所有-非左侧树：success");
        return this.response(codeVOList);
    }

    @ApiOperation(value = "02.获取左边栏-左侧字典树")
    @GetMapping(value = "/findTree", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<CodeVO>> findTree() {
        log.info("02.获取左边栏-左侧字典树");
        List<CodeVO> topByType0 = codeService.findLeftTree();
        log.info("02.获取左边栏-左侧字典树：success");
        return this.response(topByType0);
    }

    @ApiOperation(value = "03.根据type类型查询")
    @PostMapping(value = "/findByType", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<CodeVO>> findByType(@Validated @RequestBody CodeTypeQueryDTO typeQueryDTO) {
        log.info("03.根据parentType类型查询：{}", LogUtil.encode(typeQueryDTO));
        List<CodeVO> codeVOList = codeService.findByType(typeQueryDTO.getCodeType(), typeQueryDTO.getIsVisible());
        log.info("03.根据parentType类型查询：success");
        return this.response(codeVOList);
    }

    @ApiOperation(value = "03-1.根据type类型列表查询")
    @PostMapping(value = "/findByTypeList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<CodeVO>> findByTypeList(@Validated @RequestBody CodeTypeListDTO typeQueryDTO) {
        log.info("03.根据parentType类型查询：{}", LogUtil.encode(typeQueryDTO));
        List<CodeVO> codeVOList = codeService.findByTypeList(typeQueryDTO.getCodeTypeList());
        log.info("03.根据parentType类型查询：success");
        return this.response(codeVOList);
    }

    @ApiOperation(value = "04.排序,上升or下降", notes = "idOne-选中行数据的ID, idTwo-选中行上一行数据的ID(上升)或者选中行下一行数据的ID(下降)")
    @PostMapping(value = "/upAndDown", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> upAndDown(@Validated @RequestBody CodeOrderDTO orderDTO) throws GeneralSimpleException {
        log.info("04.排序,上升or下降：{}", LogUtil.encode(orderDTO));
        boolean upAndDown = codeService.upAndDown(orderDTO.getIdOne(), orderDTO.getIdTwo());
        log.info("04.排序,上升or下降：success");
        return this.response(upAndDown);
    }


    @ApiOperation(value = "05.1 新增代码")
    @PostMapping(value = "/insert", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> insert(@Validated @RequestBody CodeAddDTO dto) throws GeneralSimpleException {
        log.info("05.1新增代码：{}", LogUtil.encode(dto));
        boolean status = codeService.insert(dto);
        log.info("05.1新增代码：success");
        return this.response(status);
    }

    @ApiOperation(value = "05.2 新增子代码")
    @PostMapping(value = "/subCode/insert", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> insertSubCode(@Validated @RequestBody CodeSubCodeAddDTO dto) throws GeneralSimpleException {
        log.info("05.2 新增子代码：{}", LogUtil.encode(dto));
        boolean status = codeService.insertSubCode(dto);
        log.info("05.2 新增子代码：success");
        return this.response(status);
    }

    @ApiOperation(value = "06.1 修改代码")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> update(@Validated @RequestBody CodeUpdateDTO dto) throws GeneralSimpleException {
        log.info("06.1 修改代码：{}", LogUtil.encode(dto));
        boolean status = codeService.modify(dto);
        log.info("06.1 修改代码：success");
        return this.response(status);
    }

    @ApiOperation(value = "06.2 修改子代码")
    @PostMapping(value = "/subCode/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateSubCode(@Validated @RequestBody CodeSubCodeUpdateDTO dto) throws GeneralSimpleException {
        log.info("06.2 修改子代码：{}", LogUtil.encode(dto));
        boolean status = codeService.updateSubCode(dto);
        log.info("06.2 修改子代码：success");
        return this.response(status);
    }

    @ApiOperation(value = "07.根据id删除代码")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> deleteById(@Validated @RequestBody CodeIdDTO idDTO) {
        log.info("07.根据id删除代码：{}", LogUtil.encode(idDTO));
        boolean status = codeService.deleteById(idDTO.getId());
        log.info("07.根据id删除代码：success");
        return this.response(status);
    }

//    @ApiOperation(value = "08.根据条件查询代码项")
//    @PostMapping(value = "/findByCondition", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public ResponseResult<List<CodeBaseVO>> findByCondition(@Validated @RequestBody CodeQueryDTO dto) {
//        log.info("08.根据条件查询代码项：{}", LogUtil.encode(dto));
//        final List<CodeBaseVO> list = codeService.findByCondition(dto);
//        log.info("08.根据条件查询代码项：success");
//        return super.response(list);
//    }
//
//    @ApiOperation(value = "09.根据条件查询代码项(包括子代码)")
//    @PostMapping(value = "/findLeafTreeByCondition", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public ResponseResult<List<CodeTreeVo>> findLeafTreeByCondition(@Validated @RequestBody CodeSubCodeQueryDTO dto) {
//        log.info("09.根据条件查询代码项(包括子代码)：{}", LogUtil.encode(dto));
//        final List<CodeTreeVo> list = codeService.findLeafTreeByCondition(dto);
//        log.info("09.根据条件查询代码项(包括子代码)：success");
//        return super.response(list);
//    }
}
